package com.xsdlr;

import com.xsdlr.handler.DataHandler;
import com.xsdlr.pipline.ImageSavePipeline;
import com.xsdlr.processor.YouzyPageProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import us.codecraft.webmagic.Spider;

import javax.annotation.Resource;
import java.util.List;

@SpringBootApplication
public class SchoolPicCaptureApplication implements CommandLineRunner {
    Logger logger = LoggerFactory.getLogger(SchoolPicCaptureApplication.class);

    @Resource
    YouzyPageProcessor youzyPageProcessor;
    @Resource
    DataHandler youzySchoolReadHandler;
    @Resource
    SchoolDataReader schoolDataReader;
    @Resource
    ImageSavePipeline imageSavePipeline;
    @Value("${school.exportPath.baike}")
    String bakeBasePath;
    @Value("${school.exportPath.youzy}")
    String youzyBasePath;

	public static void main(String[] args) {
		SpringApplication.run(SchoolPicCaptureApplication.class, args);
	}

	@Override
	public void run(String... args) throws Exception {
        List<String> schoolData = schoolDataReader.parse(youzySchoolReadHandler);
        logger.info("读取学校信息条数:{}", schoolData.size());

        Spider.create(youzyPageProcessor)
                .startUrls(schoolData)
                .thread(10)
                .addPipeline(imageSavePipeline.setBasePath(youzyBasePath))
                .run();
	}
}
